package rest.jbpm.svg;

import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: xEviL
 * Date: 26.10.11
 * Time: 15:06
 * To change this template use File | Settings | File Templates.
 */
class JbpmElement {
    protected Integer x = 0;
    protected Integer y = 0;
    protected Integer w = 0;
    protected Integer h = 0;
    protected double k = 0.8;
    protected boolean active = false;
    protected String name = null;
    protected Number duration = null;

    public JbpmElement(String _name, Integer _x, Integer _y, Integer _w, Integer _h){
        name = _name;
        x = _x;
        y = _y;
        w = (int)Math.round(_w*k);
        h = (int)Math.round(_h*k);
    }

    public String draw(){
       String strokeColor = "black";
       if (active){
           strokeColor = "red";
       }
       String resp = "<rect x=\""+x+"\" y=\""+y+"\" width=\""+w+"\" height=\""+h+"\"\n" +
               "  style=\"fill:white;stroke:"+strokeColor+";stroke-width:2;fill-opacity:1;\n" +
               "  stroke-opacity:0.9\"" +
               " rx=\"10\" ry=\"10\"/>\n" +
               "<text x=\""+(x+(w/2))+"\" y=\""+(y+(h/2))+"\" font-size=\""+(k*14.0)+"px\" font-family=\"sans-serif\" fill=\"grey\" style=\"text-anchor:middle;dominant-baseline:central;\">"+name+"</text>";

        if (this != null){
            String id = "a_"+(Math.round(Math.random()*10000));
            resp+="<rect id=\"b"+id+"\" rx=\"2\" ry=\"2\" x=\""+(x+w)+"\" y=\""+(y)+"\" width=\"0\" height=\"0\" style=\"fill:white; fill-opacity:0.5;\"/>";
            resp+= "<text id=\""+id+"\" font-size=\""+(k*14)+"\" style=\"dominant-baseline:text-over-edge;\" x=\""+(x+w)+"\" y=\""+(y)+"\" font-family=\"sans-serif\" fill=\"coral\">"+this.getDuration()+"</text>\n" +
                    "<script>\n" +
                    "var "+id+" = document.getElementById(\""+id+"\"); var b"+id+" = document.getElementById(\"b"+id+"\"); var bbox = "+id+".getBBox(); b"+id+".setAttribute(\"width\",bbox.width); b"+id+".setAttribute(\"height\",bbox.height); b" + id + ".setAttribute(\"y\",bbox.y);b" + id + ".setAttribute(\"x\",bbox.x);\n" +
                    "</script>";
        }

        return resp;
    }

    public void setActive(){
        active = true;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public int getW(){
        return w;
    }

    public int getH(){
        return h;
    }

    public int getXMax(){
        return x+w+50;
    }

    public int getYMax(){
        return y+h+50;
    }

    public Integer getElCenterX(){
        return x+(w/2);
    }

    public Integer getElCenterY(){
        return y+(h/2);
    }

    public Integer getBorderX(Integer dX, Integer dY){
        //
        Integer cX = getElCenterX();
        Integer cY = getElCenterY();

        //
        double tgA = (((double) (dY-cY) / (double)(dX-cX)));

        double A = Math.atan( tgA );

        if ( Math.abs(A) < (Math.atan((double)h/(double)w))){
            // точка примыкает к вертикальным стенкам "коробки"
            return (cX - (cX-dX)/Math.abs(cX-dX)*(w/2));

        }else{
            // точка примыкает к горизонтальным стенкам "коробки"
            return  cX - (int)Math.round((cX-dX)/Math.abs(cX-dX)*Math.abs(((double)((h/2)))*(Math.tan(Math.PI/2- A))));
        }
    }

    public Integer getBorderY(Integer dX, Integer dY){
        //
        Integer cX = getElCenterX();
        Integer cY = getElCenterY();

        //
        Double tgA = (((double) (dY-cY) / (double)(dX-cX)));

        Double A = Math.atan( tgA );

        if ( Math.abs(A) > (Math.atan((double)h/(double)w))){
            // точка примыкает к вертикальным стенкам "коробки"
            return (cY - (cY-dY)/Math.abs(cY-dY)*(h/2));

        }else{
            // точка примыкает к горизонтальным стенкам "коробки"
            return cY - (int)Math.round((cY-dY)/Math.abs(cY-dY)*Math.abs(((double)( (w/2)))*(Math.tan(A))));
        }
    }

    public double getBorderAngle(Integer dX, Integer dY) {
        Integer cX = getElCenterX();
        Integer cY = getElCenterY();

        Double tgA = (((double) (dY-cY) / (double)(dX-cX)));

        Double A = Math.toDegrees(Math.atan( tgA ));

        return A + 90*(1+(cX-dX)/Math.abs(cX-dX));
    }

    public void setDuration(Number d){
        duration = d;
    }

    public String getDuration(){

        if (duration == null)
            return "";

        if (TimeUnit.MILLISECONDS.toDays(duration.longValue()) > 0)
            return "~"+TimeUnit.MILLISECONDS.toDays(duration.longValue())+"дн.";
        if (TimeUnit.MILLISECONDS.toHours(duration.longValue()) > 0)
            return "~"+TimeUnit.MILLISECONDS.toHours(duration.longValue())+"ч.";
        if (TimeUnit.MILLISECONDS.toMinutes(duration.longValue()) > 0)
            return "~"+TimeUnit.MILLISECONDS.toMinutes(duration.longValue())+"мин.";

        return "";
    }
}
